package io.characterStream.fileRorW.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.function.BiConsumer;

/*
统计文件中每一个字符出现的次数，并展示
A(1)B(2)C(7)
 */
public class test4 {
    public static void main(String[] args) throws IOException {
        //采用字符流
        FileReader fr = new FileReader("E:\\B.txt");
        HashMap<Character,Integer> hm = new HashMap<>();
        int i;
        while ((i=fr.read())!=-1){
            char c = (char)i;
            if(hm.containsKey(c)){
                hm.put(c,hm.get(c)+1);
            }else {
                hm.put(c,1);
            }
        }
        fr.close();
        StringBuilder sb = new StringBuilder();
        hm.forEach(new BiConsumer<Character,Integer>(){
            @Override
            public void accept(Character key, Integer value) {
                //采用StringBuilder便于拼接
                sb.append(key).append("(").append(value).append(")");
            }
        });
        System.out.println(sb);
    }
}
